<?php

/**
 * @author	barbarosalcin
 * @package	general
 * @desc	index
 * @version	$Id: 20120113
 * @todo	documentation
 */

//set_time_limit(0);
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);

mb_internal_encoding('UTF-8');

define('DS', DIRECTORY_SEPARATOR);
define('__PHPROOT__', 'php/');

// Included config


require_once (__PHPROOT__ . 'config/inc.config.php');
require_once (__PHPROOT__ . 'config/inc.constants.php');
function myautoload($cls)
{
    $_cls = strtolower($cls);
    $_cls = __PHPROOT__ . 'classes/class.' . $_cls . '.php';
    
    if (file_exists($_cls))
        require_once $_cls;
}

spl_autoload_register('myautoload');

try
{
    $tk = new core($config);
}
catch ( myexception $e )
{
    unset($config['db']);
    echo $e->errormessage();
    exit();
}

unset($config['db']);

$tk->config['login_required'] = $tk->config['login_required'] != '' ? explode(",", $tk->config['login_required']) : array();

$config = array_merge($config, $tk->config);

include (__PHPROOT__ . 'init.php');

$tk->eh->lang = $lang;

/* @todo: launch only if postman needed 
$tk->postman = new postman();
$tk->postman->from = $tk->config['contact_mail'];
$tk->postman->fromname = $tk->config['contact_name'];
$tk->postman->mailer = 'mail';
$tk->postman->vars = array();
$tk->postman->tpl_path = $tk->template_dir . $config['template_name'] . '/email/' . $req['language'] . '/';
*/

/*
 * Depending of subpage variable, checking if we have such file in pages subdirectory
 * If not, display default
 */

$user_ip = $tk->f->encode_ip($client_ip);

$tk->session->pagestart($user_ip, $req["subpage"]);

$tk->page->request = ($req['ajax']) ? 'json' : 'html';

$req['subpage'] = (in_array($req['subpage'], $tk->config['login_required']) && ! $tk->session->data['session_logged_in']) ? 'login' : $req['subpage'];

$tmp = ($req["subpage"] == 'admin') ? '_admin' : '_core';

$tk->setBase($tmp)->setLocation($config['default_language'])->setTheme($config['template_name']);

unset($tmp);

$sql = "SELECT page_url FROM " . CONTENT_TABLE . " WHERE page_group = 'user'";

if (! ($result = $tk->db->sql_query($sql)))
{
    $error = 'sql';
    $error_msg = $tk->db->sql_error();
    $tk->eh->message_die($req['subpage'], $error, __LINE__, __FILE__, $sql . "\n" . $error_msg['message']);
    goto compile;
}

while($row = $tk->db->sql_fetchrow($result))
{
    $tmp[] = $row['url'];
}

if (in_array($req['subpage_orig'], $tmp))
{
    $req['subpage'] = 'content';
}

$req["subpage"] = (isset($req["subpage"]) && is_file(__PHPROOT__ . 'pages/page_' . $req["subpage"] . '.php')) ? $req["subpage"] : 'default';

include (__PHPROOT__ . 'pages/page_' . $req["subpage"] . '.php');

$tk->page->_pn = $req["subpage"];
$tk->page->_pv['data'] = $data;

array_push($tk->cache, md5($req["uri"]), md5(implode('_', $req["get"])), $tk->page->request);

$themePath = $tk->getTemplateDir('templates') . $tk->getTheme();
$tmp = $themePath . '/inc/inc_header.tpl';

if (is_file($tmp) && $tk->getBase() != '_admin')
{
    $tk->page->_pv['inc_header'] = '../' . $tk->getTheme() . '/inc/inc_header.tpl';
}

$tmp = $themePath . '/inc/inc_footer.tpl';

if (is_file($tmp) && $tk->getBase() != '_admin')
{
    $tk->page->_pv['inc_footer'] = '../' . $tk->getTheme() . '/inc/inc_footer.tpl';
}

unset($tmp);

$tk->configLoad($tk->getTemplateDir('templates') . '/' . $tk->getBase() . '/template.conf', 'images');

$tk->init_modules($tk->page->_pn);

compile:

/**
 * @desc Main Smarty Variables that will be used by Core
 */

if ($req['ajax'] != '1' && 0)
{
    $tk->debugging = TRUE;
}

$tk->assign('lang', $lang);
$tk->assign('action', $req['action']);
$tk->assign('req', $req);
$tk->assign('config', $config);
$tk->assign('subpage', $req["subpage"]);

$tmp = $tk->config['docroot'] . str_replace('\\', '/', $tk->getTemplateDir('templates'));

$tk->assign('user', $tk->session->data);
$tk->assign('templates', $tmp);
$tk->assign('theme', $tk->getTheme());
$tk->assign('location', $tk->getLocation());
$tk->assign('base', $tk->getBase());
$tk->assign('themePath', $themePath . '/');
$tk->assign('basePath', $tmp . $tk->getBase());
$tk->assign('imagesDir', $tmp . $tk->getBase() . '/images');

/* page vars */

$tk->page->_pv['page_title'] = ($tk->page->_pv['page_title'] != '') ? str_replace('-', ' ', $tk->page->_pv['page_title']) : $tk->page->_pn;
$tk->page->_pv['page_keywords'] = ($tk->page->_pv['page_keywords'] != '') ? $tk->page->_pv['page_keywords'] : $config['keywords'];
$tk->page->_pv['page_description'] = ($tk->page->_pv['page_description'] != '') ? $tk->page->_pv['page_description'] : $config['description'];

$tk->assign('pn', $tk->page->_pn);
$tk->assign('pv', $tk->page->_pv);
$tk->assign('pr', $tk->page->request);
/* page modules */
$tk->assign('pm', $tk->page->_pm);

$tk->assign('query_log', $tk->db->query_log);

ob_end_clean();
ob_start();
try
{
    if ($tk->eh->_errorFlag)
    {
        $tk->_error();
    }
    else
    {
        $tk->_display();
    }
}
catch ( myexception $e )
{
    ob_end_flush();
    $e->errormessage();
    exit();
}

/*
* Close our DB connection.
*/

$tk->db->sql_close();

//
// Compress buffered output if required and send to browser
//


$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];

if (headers_sent())
    $encoding = false;

else if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false)
    $encoding = false;

else if (strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false)
    $encoding = 'gzip';
else
    $encoding = false;

$ob_contents = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', ob_get_clean());

header("Content-type: " . $tk->page->contentType);
$encoding = false;
if ($encoding)
{
    $ob_strlen = strlen($ob_contents);
    if ($ob_strlen >= 2048 && $config['gzip_compress'])
    {
        header('Content-Encoding: ' . $encoding);
        echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
        $ob_contents = gzcompress($ob_contents, 9);
        $ob_contents = substr($ob_contents, 0, $ob_strlen);
    }
}

echo $ob_contents;
exit();
